package com.yonyougov.devops.cruxcisupport.rolebutton;

import com.yonyougov.devops.cruxcisupport.rolebutton.dto.AddMulRoleButtonDto;
import com.yonyougov.devops.cruxcisupport.rolebutton.dto.AddMulRoleButtonRequestDto;
import com.yonyougov.devops.cruxcisupport.rolebutton.dto.RoleButtonDeleteBatchRequestDto;
import com.yonyougov.devops.cruxcisupport.rolebutton.dto.RoleButtonPageLikeRequestDto;
import com.yonyougov.devops.cruxcisupport.rolebutton.vo.RoleButtonPageLikeDto;
import com.yonyougov.devops.cruxcisupport.rolebutton.vo.RoleButtonPageLikeVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(value = "角色按钮关联", tags = "角色按钮关联")
@RestController
@RequestMapping("/api/v2/rolebutton")
public class RoleButtonController extends CrudController<RoleButtonEntity, BigInteger> {

    private RoleButtonService roleButtonService;

    private RoleButtonDao roleButtonDao;

    RoleButtonController(RoleButtonService roleButtonService, RoleButtonDao roleButtonDao) {
        this.roleButtonService = roleButtonService;
        this.roleButtonDao = roleButtonDao;
    }

    @Override
    public BaseService<RoleButtonEntity, BigInteger> getService() {
        return roleButtonService;
    }

    //<!--用户自定义代码开始-->

    @ApiModelProperty(value = "角色模块关联按钮调用", notes = "批量新增关联")
    @PostMapping(value = "/rb/add/batch")
    public BaseResponseDto addMulRoleButton(@RequestBody @Valid AddMulRoleButtonRequestDto request) {
        AddMulRoleButtonDto addMulRoleButtonDto = AddMulRoleButtonDto.builder().build();
        BeanUtils.copyProperties(request, addMulRoleButtonDto);

        roleButtonService.addMulRoleButton(addMulRoleButtonDto);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiModelProperty(value = "角色模块关联按钮调用")
    @PostMapping(value = "/rb/delete/batch/ids")
    @SneakyThrows
    public BaseResponseDto DeleteBatchRoleButtonByIds(@RequestBody @Valid RoleButtonDeleteBatchRequestDto request) {
        roleButtonService.deleteBatchRoleButtonByIds(request.getIds());

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiModelProperty(value = "角色模块关联按钮调用", notes = "可选择根据角色id和按钮名称分页模糊查询")
    @PostMapping(value = "/rb/page/like")
    public BaseResponseDto findRoleButtonPageLike(@RequestBody @Valid RoleButtonPageLikeRequestDto request) {
        RoleButtonPageLikeDto roleButtonPageLikeDto = new RoleButtonPageLikeDto();
        BeanUtils.copyProperties(request, roleButtonPageLikeDto);

        Pagination<RoleButtonPageLikeVo> pagination = roleButtonService.findRoleButtonPageLike(roleButtonPageLikeDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }
    //<!--用户自定义代码结束-->
}
